﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using NT.SharePoint.RS.WebParts.ToolParts;

namespace NT.SharePoint.RS.WebParts.ToolParts
{
    public class RSPropertiesToolPart : ToolPart
    {
        public RSPropertiesToolPart()
        {
            this.Title = "RS Report Properties";
        }

        public string ReportPath
        {
            get
            {
                return this.tbReportPath.Text;
            }
        }

        TextBox tbReportPath;

        protected override void CreateChildControls()
        {
            WPRSView wp = this.ParentToolPane.SelectedWebPart as WPRSView;

            Literal l = new Literal();
            l.Text = "Report Path:<br>";
            this.Controls.Add(l);

            tbReportPath = new TextBox();
            tbReportPath.ID = "tbReportPath";
            tbReportPath.Text = wp.ReportPath;
            this.Controls.Add(tbReportPath);

            l = new Literal();
            l.Text = "<br><br>";
            this.Controls.Add(l);

            LinkButton lb = new LinkButton();
            lb.Text = "Update Parameters";
            lb.Click += new EventHandler(lb_Click);
            this.Controls.Add(lb);
        }

        void lb_Click(object sender, EventArgs e)
        {
            //Refresh parameter if needed
            ((RSParametersToolPart)this.ParentToolPane.ToolParts[1]).ResfreshDisplay(tbReportPath.Text);
        }

        public override void ApplyChanges()
        {
            WPRSView wp = this.ParentToolPane.SelectedWebPart as WPRSView;

            wp.ReportPath = tbReportPath.Text;

            //Refresh parameter if needed
            ((RSParametersToolPart)this.ParentToolPane.ToolParts[1]).ResfreshDisplay(tbReportPath.Text);

            //Refresh the cache (put here as it's the last apply called)
            wp.Page.Cache.Remove("RSParameters" + wp.ID);
            wp.DataBind();
        }
    }
}
